package 
{
	import br.com.jomos.aQuery.Selector;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Jomos
	 */
	public class Main extends Sprite 
	{
		
		public var MC_1:Sprite;
		public var MC_2:Sprite;
		public var MC_3:Sprite;
		public var MC_4:Sprite;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			MC_1 = new MovieClip();
			MC_1.graphics.beginFill(0xff0000);
			MC_1.graphics.drawCircle(0, 0, 80);
			
			MC_2 = new Sprite();
			MC_2.graphics.beginFill(0x00ff00);
			MC_2.graphics.drawCircle(0, 0, 80);
			
			MC_3 = new MovieClip();
			MC_3.graphics.beginFill(0x0000ff);
			MC_3.graphics.drawCircle(0, 0, 80);
			
			MC_4 = new Sprite();
			MC_4.graphics.beginFill(0xff00ff);
			MC_4.graphics.drawCircle(0, 0, 80);
			
			
			addChild(MC_1);
			addChild(MC_2);
			addChild(MC_3);
			addChild(MC_4);
			
			MC_1.x = MC_1.y = 90;
			MC_2.x = MC_2.y = 180+35;
			MC_3.x = MC_3.y = 270+65;
			MC_4.x = MC_4.y = 270 + 90 + 90;
			
			generateRandomChilds(MC_1, 11,Shape);
			generateRandomChilds(MC_2, 14,Sprite);
			generateRandomChilds(MC_3, 16,MovieClip);
			generateRandomChilds(MC_4, 19,Shape);

			stage.addEventListener(MouseEvent.CLICK, teste);
		}
		
		private function teste(e:MouseEvent):void 
		{
			
			var s:Selector = new Selector(this).children().children(Shape);
			for each(var tempChild:DisplayObject in s.selected)
			{
				tempChild.alpha = .1;
			}
		}
		
		
			
		private function generateRandomChilds(hisParent:DisplayObjectContainer, numChildrens:int,displayClass:Class):Array 
		{
			
			var sinteticChildrens:Array = new Array();
			
			var tempChild:*;
			var randShape:int;
			var randColor:uint;
			while (numChildrens)
			{
				randColor = 0xFFFFFF * Math.random();
				randShape = 3 * Math.random();

				tempChild = new displayClass();
				tempChild.graphics.beginFill(randColor, .9);
				
				if (randShape == 0) tempChild.graphics.drawCircle(0, 0, 10 + Math.random() * 30);
				else if (randShape == 1) tempChild.graphics.drawEllipse(0, 0, 10 + Math.random() * 30, 10 + Math.random() * 30);
				else tempChild.graphics.drawRect(0, 0, 10 + Math.random() * 30, 10 + Math.random() * 30);
				
				tempChild.x = hisParent.width * Math.random();
				tempChild.y = hisParent.height * Math.random();
				
				hisParent.addChild(tempChild);
				
				sinteticChildrens.push(tempChild);
				
				numChildrens--;
			}
			
			return sinteticChildrens;
		}
		
	}
	
}